package lib.bone.system.extend.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lib.bone.system.extend.domain.dto.*;
import lib.bone.system.extend.domain.entity.SysDictData;
import lib.bone.system.extend.domain.entity.SysDictType;
import lib.bone.system.extend.service.impl.SysDictServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author : kindear
 * @Date : 2021-12-30
 * @Desc : 字典维护
 */
@Api(tags = "系统字典管理")
@RestController
@RequestMapping("system/dict")
public class SysDictController {

    @Autowired
    SysDictServiceImpl sysDictService;

    @GetMapping("data/type/{dictType}")
    @ApiOperation(value = "字典检索",notes = "根据字典类型检索对应数据")
    public ResponseDTO getDictDataByDictType(@PathVariable("dictType") String dictType){
        //data 不区分单复数
        List<SysDictData> sysDictData = sysDictService.findByDictType(dictType);
        return ResponseDTO.succData(sysDictData);
    }

    @GetMapping("types/{dictId}")
    @ApiOperation(value = "查询单条字典类型",notes = "")
    public ResponseDTO getDictType(@PathVariable("dictId") Long dictId){
        Optional<SysDictType> sysDictTypeOp = sysDictService.getDictType(dictId);
        if(!sysDictTypeOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        return ResponseDTO.succData(sysDictTypeOp.get());
    }
    @DeleteMapping("types/{dictId}")
    @ApiOperation(value = "删除单条字典类型",notes = "")
    public ResponseDTO deleteDictType(@PathVariable("dictId") Long dictId){
        return sysDictService.deleteDictType(dictId);
    }

    @PostMapping("types")
    @ApiOperation(value = "新建单条字典类型",notes = "")
    public ResponseDTO saveDictType(@RequestBody DictTypeAddDTO addDTO){
        return sysDictService.saveDictType(addDTO);
    }
    @PutMapping("types")
    @ApiOperation(value = "更新单条字典类型",notes = "")
    public ResponseDTO updateDictType(@RequestBody DictTypeUpdateDTO updateDTO){
        return sysDictService.updateDictType(updateDTO);
    }


    @GetMapping("types")
    @ApiOperation(value = "字典类型查询",notes = "")
    public ResponseDTO getDictTypes(DictTypeQueryDTO queryDTO){
        Sort sort;
        if(queryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, queryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, queryDTO.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(queryDTO.getPageNum() - 1 , queryDTO.getPageSize(), sort);

        SysDictType sysDictType = new SysDictType();
        BeanUtil.copyProperties(queryDTO,sysDictType, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<SysDictType> example = Example.of(sysDictType);

        //return nepuPaperDictService.getAll(pageable,example);
        return sysDictService.getDictTypes(pageable,example);
    }

    @PostMapping("data")
    @ApiOperation(value = "新建字典数据",notes = "传入数据，新建字典数据")
    public ResponseDTO saveDictData(@RequestBody DictDataAddDTO dictDataAddDTO){
        return sysDictService.saveDictData(dictDataAddDTO);
    }

    @PutMapping("data")
    @ApiOperation(value = "更新字典数据",notes = "")
    public ResponseDTO updateDictData(@RequestBody DictDataUpdateDTO updateDTO){
        return sysDictService.updateDictData(updateDTO);
    }

    @GetMapping("data/{dataId}")
    @ApiOperation(value = "获取单条字典数据",notes = "")
    public ResponseDTO getDictData(@PathVariable("dataId") Long dataId){
        Optional<SysDictData> sysDictDataOp = sysDictService.getDictData(dataId);
        if(!sysDictDataOp.isPresent())
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        return ResponseDTO.succData(sysDictDataOp.get());
    }

    @DeleteMapping("data/{dataId}")
    @ApiOperation(value = "删除单条字典数据",notes = "")
    public ResponseDTO deleteDictData(@PathVariable("dataId") Long dataId){

        return sysDictService.deleteDictData(dataId);
    }

}
